﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using JXT.PrimaryKey.Batman.Log;
using JXT.PrimaryKey.Batman.Core.Extensions;
using log4net;
using log4net.Config;

//注意，此处配置log4net配置文件为以.开头的相对地址，部署时请确保地址的一致性
//[assembly: XmlConfigurator(Watch = true, ConfigFile = "config\\log.config")]
namespace JXT.PrimaryKey.Batman.Core.Log
{
    /// <summary>
    /// Log4net日志系统
    /// </summary>
    public class Log4Logger : ILogger
    {
        private static readonly string _configFile = String.Format("config{0}log.config", Path.DirectorySeparatorChar);

        private static readonly string _defaultLoggerName = "defaultLogger";

        private readonly ILog _innerLogger;

        static Log4Logger()
        {
            //改为使用路径初始化
            try
            {
                string path = Path.Combine(SystemEnvironment.ApplicationBase, _configFile);
                if (!File.Exists(path))
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("can not find log4net config file on {0}.", path));
                }
                else
                {
                    XmlConfigurator.ConfigureAndWatch(new FileInfo(path));
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("init log4logger error for {0}", e.Message));
            }
        }

        protected Log4Logger(string name)
        {
            _innerLogger = LogManager.GetLogger(name);
        }

        public Log4Logger()
            : this(_defaultLoggerName)
        {
        }

        public void Write(string message, Exception exception = null, LogLevel level = LogLevel.Message)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    _innerLogger.Debug(message, exception);
                    break;
                case LogLevel.Error:
                    _innerLogger.Error(message, exception);
                    break;
                case LogLevel.Fatal:
                    _innerLogger.Fatal(message, exception);
                    break;
                case LogLevel.Message:
                    _innerLogger.Info(message, exception);
                    break;
                case LogLevel.Wraning:
                    _innerLogger.Warn(message, exception);
                    break;
            }
        }

        public void Message(string message, Exception exception = null)
        {
            Write(message, exception, LogLevel.Message);
        }

        public void Debug(string message, Exception exception = null)
        {
            Write(message, exception, LogLevel.Debug);
        }

        public void Wraning(string message, Exception exception = null)
        {
            Write(message, exception, LogLevel.Wraning);
        }

        public void Error(string message, Exception exception = null)
        {
            Write(message, exception, LogLevel.Error);
        }

        public void Fatal(string message, Exception exception = null)
        {
            Write(message, exception, LogLevel.Fatal);
        }
    }
}
